package com.ruoyi.custom.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.custom.domain.Custom;
import com.ruoyi.custom.domain.vo.TbBackVo;
import com.ruoyi.custom.service.ICustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.custom.mapper.TbBackMapper;
import com.ruoyi.custom.domain.TbBack;
import com.ruoyi.custom.service.ITbBackService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 客户回款Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-25
 */
@Service
public class TbBackServiceImpl extends ServiceImpl<TbBackMapper, TbBack> implements ITbBackService
{
    @Autowired
    private TbBackMapper tbBackMapper;
    @Autowired
    private ICustomService customService;

    /**
     * 查询客户回款
     *
     * @param id 客户回款主键
     * @return 客户回款
     */
    @Override
    public TbBack selectTbBackById(Long id)
    {
        return tbBackMapper.selectTbBackById(id);
    }

    /**
     * 查询客户回款列表
     *
     * @param tbBack 客户回款
     * @return 客户回款
     */
    @Override
    public List<TbBack> selectTbBackList(TbBack tbBack)
    {
        return tbBackMapper.selectTbBackList(tbBack);
    }

    /**
     * 新增客户回款
     *
     * @param tbBack 客户回款
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTbBack(TbBack tbBack)
    {
        tbBack.setCreateBy(getUsername());
        tbBack.setCreateTime(DateUtils.getNowDate());
        int result = tbBackMapper.insertTbBack(tbBack);
        //修改客户表状态
        Custom custom = new Custom();
        custom.setId(Long.valueOf(tbBack.getCustomId()));
        custom.setStatus("3");
        customService.updateCustom(custom);
        return result;
    }

    /**
     * 修改客户回款
     *
     * @param tbBack 客户回款
     * @return 结果
     */
    @Override
    public int updateTbBack(TbBack tbBack)
    {
        return tbBackMapper.updateTbBack(tbBack);
    }

    /**
     * 批量删除客户回款
     *
     * @param ids 需要删除的客户回款主键
     * @return 结果
     */
    @Override
    public int deleteTbBackByIds(Long[] ids)
    {
        return tbBackMapper.deleteTbBackByIds(ids);
    }

    /**
     * 删除客户回款信息
     *
     * @param id 客户回款主键
     * @return 结果
     */
    @Override
    public int deleteTbBackById(Long id)
    {
        return tbBackMapper.deleteTbBackById(id);
    }

    public List<TbBackVo> getRateMoneyRank(){
        return tbBackMapper.getRateMoneyRank();
    }

    public List<TbBackVo> getDeptRateMoneyRank(){
        return tbBackMapper.getDeptRateMoneyRank();
    }
}
